home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / p-interp.lha / p-interp-0.5 / Search.c < prev    next >
C/C++ Source or Header  |  2001-05-26  |  7KB  |  281 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.   $Log: Search.c,v $
  21.   Revision 1.3  2001/05/26 15:13:29  mario
  22.   Diverse kleine Fehler behoben, fehlende #includes, Labels ohne Statement
  23.   dahinter, ...
  24.  
  25.   Revision 1.2  2001/05/20 13:12:02  mario
  26.   CVS-Idents und Logs eingefügt
  27.  
  28.  
  29. */
  30.  
  31. #ident "$Id: Search.c,v 1.3 2001/05/26 15:13:29 mario Exp $";
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #include "psystem.h"
  37. #include "Memory.h"
  38. #include "Array.h"
  39.  
  40. #undef DEBUG
  41.  
  42. /* 
  43.  
  44.    IdSearch() 
  45.  
  46.    Details about IdSearch can be found in
  47.    http://www.gno.org/pub/apple2/doc/apple/technotes/pasc/tn.pasc.014 
  48.  
  49. */
  50.  
  51. #define SYM_IDENT    0
  52. #define SYM_COMMA    1
  53. #define SYM_COLON    2
  54. #define SYM_SEMICOLON    3
  55. #define SYM_LPARENT    4
  56. #define SYM_RPARENT    5
  57. #define SYM_DO        6
  58. #define SYM_TO        7
  59. #define SYM_DOWNTO    8
  60. #define SYM_END        9
  61. #define SYM_UNTIL    10
  62. #define SYM_OF        11
  63. #define SYM_THEN    12
  64. #define SYM_ELSE    13
  65. #define SYM_BECOMES    14
  66. #define SYM_LBRACK    15
  67. #define SYM_RBRACK    16
  68. #define SYM_ARROW    17
  69. #define SYM_PERIOD    18
  70. #define SYM_BEGIN    19
  71. #define SYM_IF        20
  72. #define SYM_CASE    21
  73. #define SYM_REPEAT    22
  74. #define SYM_WHILE    23
  75. #define SYM_FOR        24
  76. #define SYM_WITH    25
  77. #define SYM_GOTO    26
  78. #define SYM_LABEL    27
  79. #define SYM_CONST    28
  80. #define SYM_TYPE    29
  81. #define SYM_VAR        30
  82. #define SYM_PROC    31
  83. #define SYM_FUNC    32
  84. #define SYM_PROG    33
  85. #define SYM_FORWARD    34
  86. #define SYM_INTCONST    35
  87. #define SYM_REALCONST    36
  88. #define SYM_STRINGCONST    37
  89. #define SYM_NOT        38
  90. #define SYM_MULOP    39
  91. #define SYM_ADDOP    40
  92. #define SYM_RELOP    41
  93. #define SYM_SET        42
  94. #define SYM_PACKED    43
  95. #define SYM_ARRAY    44
  96. #define SYM_RECORD    45
  97. #define SYM_FILE    46
  98. #define SYM_OTHER    47
  99. #define SYM_LONGCONST    48
  100. #define SYM_USES    49
  101. #define SYM_UNIT    50
  102. #define SYM_INTER    51
  103. #define SYM_IMPLE    52
  104. #define SYM_EXTERNL    53
  105. #define SYM_OTHERW    54
  106.  
  107. #define OP_MUL        0
  108. #define OP_RDIV        1
  109. #define OP_ANDOP    2
  110. #define OP_IDIV        3
  111. #define OP_IMOD        4
  112. #define OP_PLUS        5
  113. #define OP_MINUS    6
  114. #define OP_OROP        7
  115. #define OP_LTOP        8
  116. #define OP_LEOP        9
  117. #define OP_GEOP        10
  118. #define OP_GTOP        11
  119. #define OP_NEOP        12
  120. #define OP_EQOP        13
  121. #define OP_INOP        14
  122. #define OP_NOOP        15
  123.  
  124. struct IdTable
  125. {
  126.   byte    Token[8];
  127.   byte    Sym;
  128.   byte    Op;
  129. };
  130.  
  131. static struct IdTable IdTable[]={
  132.   { "AND     ", SYM_MULOP,    OP_ANDOP},
  133.   { "ARRAY   ", SYM_ARRAY,    OP_NOOP},
  134.   { "BEGIN   ",    SYM_BEGIN,    OP_NOOP},
  135.   { "CASE    ",    SYM_CASE,    OP_NOOP},
  136.   { "CONST   ", SYM_CONST,    OP_NOOP},
  137.   { "DIV     ", SYM_MULOP,    OP_IDIV},
  138.   { "DO      ", SYM_DO,        OP_NOOP},
  139.   { "DOWNTO  ", SYM_DOWNTO,    OP_NOOP},
  140.   { "ELSE    ", SYM_ELSE,    OP_NOOP},
  141.   { "END     ", SYM_END,    OP_NOOP},
  142.   { "EXTERNAL", SYM_EXTERNL,    OP_NOOP},
  143.   { "FILE    ", SYM_FILE,    OP_NOOP},
  144.   { "FOR     ", SYM_FOR,    OP_NOOP},
  145.   { "FORWARD ", SYM_FORWARD,    OP_NOOP},
  146.   { "FUNCTION", SYM_FUNC,    OP_NOOP},
  147.   { "GOTO    ", SYM_GOTO,    OP_NOOP},
  148.   { "IF      ", SYM_IF,        OP_NOOP},
  149.   { "IMPLEMEN", SYM_IMPLE,    OP_NOOP},
  150.   { "IN      ", SYM_RELOP,    OP_INOP},
  151.   { "INTERFAC", SYM_INTER,    OP_NOOP},
  152.   { "LABEL   ", SYM_LABEL,    OP_NOOP},
  153.   { "MOD     ", SYM_MULOP,    OP_IMOD},
  154.   { "NOT     ", SYM_NOT,    OP_NOOP},
  155.   { "OF      ", SYM_OF,        OP_NOOP},
  156.   { "OR      ", SYM_ADDOP,    OP_OROP},
  157.   { "PACKED  ", SYM_PACKED,    OP_NOOP},
  158.   { "PROCEDUR", SYM_PROC,    OP_NOOP},
  159.   { "PROGRAM ", SYM_PROG,    OP_NOOP},
  160.   { "RECORD  ", SYM_RECORD,    OP_NOOP},
  161.   { "REPEAT  ", SYM_REPEAT,    OP_NOOP},
  162.   { "SEGMENT ", SYM_PROG,    OP_NOOP},
  163.   { "SET     ", SYM_SET,    OP_NOOP},
  164.   { "THEN    ", SYM_THEN,    OP_NOOP},
  165.   { "TO      ", SYM_TO,        OP_NOOP},
  166.   { "TYPE    ", SYM_TYPE,    OP_NOOP},
  167.   { "UNIT    ", SYM_UNIT,    OP_NOOP},
  168.   { "UNTIL   ", SYM_UNTIL,    OP_NOOP},
  169.   { "USES    ", SYM_USES,    OP_NOOP},
  170.   { "VAR     ", SYM_VAR,    OP_NOOP},
  171.   { "WHILE   ", SYM_WHILE,    OP_NOOP},
  172.   { "WITH    ", SYM_WITH,    OP_NOOP}
  173. };
  174.  
  175. void CspIdSearch(word BufPtr, word Arg2Ptr)
  176. {
  177.   word    BufOffset = MemRd(Arg2Ptr);
  178.   byte    TokenBuf[8];
  179.   byte    Ch;
  180.   int    Idx;
  181.   struct IdTable *idp;
  182.  
  183.   memset(TokenBuf, ' ', sizeof(TokenBuf));
  184.  
  185.   Idx=0;
  186.  
  187.   while (1)
  188.     {
  189.       Ch=MemRdByte(BufPtr, BufOffset);
  190.       if (Ch!='_')
  191.     {
  192.       if ((Ch>='a') && (Ch <='z'))
  193.         Ch -= 0x20;
  194.       else if ( ((Ch<'A') || (Ch>'Z')) &&
  195.             ((Ch<'0') || (Ch>'9')) )
  196.         break;
  197.       if (Idx<sizeof(TokenBuf))
  198.         TokenBuf[Idx++]=Ch;
  199.     }
  200.       BufOffset++;
  201.     }
  202.   BufOffset--;                /* Offset Korrigieren, dieser Buch- */
  203.                     /* stabe ist durchgefallen        */
  204.   MemWr(Arg2Ptr, BufOffset);
  205. #ifdef DEBUG
  206.   for (Idx=0;Idx<sizeof(TokenBuf); Idx++)
  207.     putchar(TokenBuf[Idx]);
  208. #endif
  209.  
  210.   for (idp=IdTable; idp<&IdTable[NUMBER(IdTable)]; idp++)
  211.     {
  212.       for (Idx=0;Idx<sizeof(TokenBuf); Idx++)
  213.     if (TokenBuf[Idx] != idp->Token[Idx])
  214.       goto next;
  215. #ifdef DEBUG
  216.       printf(": found, Sym=%d Op=%d\n", idp->Sym, idp->Op);
  217. #endif
  218.       MemWr(WordIndexed(Arg2Ptr,1), idp->Sym);
  219.       MemWr(WordIndexed(Arg2Ptr,2), idp->Op);
  220.       return;
  221.     next:
  222.       ;
  223.     }
  224.  
  225.   MemWr(WordIndexed(Arg2Ptr,1), SYM_IDENT );
  226.   MemWr(WordIndexed(Arg2Ptr,2), OP_NOOP );
  227.   for (Idx=0;Idx<sizeof(TokenBuf); Idx++)
  228.     MemWrByte(WordIndexed(Arg2Ptr,3),Idx, TokenBuf[Idx] );
  229. #ifdef DEBUG
  230.   printf(": not found\n");
  231. #endif
  232. } /* CspIdSearch() */
  233.  
  234. word CspTreeSearch(word TokenBuf, word ResultPtr, word NodePtr)
  235. {
  236.   word    Link;
  237.  
  238. #ifdef DEBUG
  239.   int i;
  240.  
  241.   for (i=0; i<8;i++)
  242.     putchar(MemRdByte(TokenBuf, i));
  243.   printf(": ");
  244. #endif
  245.  
  246.   while (1)
  247.     {
  248.       int cmp=ByteCmp(TokenBuf, NodePtr, 8);
  249.       if (cmp<0)
  250.     if ((Link=MemRd(WordIndexed(NodePtr,5))))
  251.       NodePtr=Link;            /* follow RightLink            */
  252.     else
  253.       {
  254. #ifdef DEBUG
  255.         printf("not found, should be on right node\n");
  256. #endif
  257.         MemWr(ResultPtr, NodePtr);
  258.         return(0xffff);
  259.       }
  260.       else if (cmp>0)
  261.     if ((Link=MemRd(WordIndexed(NodePtr,4))))
  262.       NodePtr=Link;            /* follow LeftLink            */
  263.     else
  264.       {
  265. #ifdef DEBUG
  266.         printf("not found, should be on left node\n");
  267. #endif
  268.         MemWr(ResultPtr, NodePtr);
  269.         return(1);
  270.       }
  271.       else
  272.     {
  273. #ifdef DEBUG
  274.       printf("found\n");
  275. #endif
  276.       MemWr(ResultPtr, NodePtr);
  277.       return(0);
  278.     }
  279.     }
  280. } /* CspTreeSearch() */
  281.